package com.geniusk.system.service.impl;

import com.geniusk.system.model.mapper.ModelPropertyMapper;
import com.geniusk.system.model.mapper.PropertyActionMapper;
import com.geniusk.system.model.pojo.PropertyAction;
import com.geniusk.system.service.PropertyActionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PropertyActionServiceImpl implements PropertyActionService {

    @Autowired
    private PropertyActionMapper propertyActionMapper;

    @Autowired
    private ModelPropertyMapper modelPropertyMapper;

    @Override
    public int insertPropertyAction(Long actionId, Long[] propertyIds) {
        propertyActionMapper.deletePropertyActionByActionId(actionId);
        if (propertyIds != null && propertyIds.length > 0) {
            //判断角色是否存在
            boolean isExist = modelPropertyMapper.selectPropertyIsExistByIds(propertyIds);
            if (isExist) {
                // 新增用户与角色管理
                List<PropertyAction> list = new ArrayList<>();
                for (Long propertyId : propertyIds) {
                    PropertyAction propertyAction = new PropertyAction();
                    propertyAction.setModelActionId(actionId);
                    propertyAction.setModelPropertyId(propertyId);
                    list.add(propertyAction);
                }
                return propertyActionMapper.insertPropertyAction(list);
            }
        }
        return 0;
    }
}
